from rest_framework import serializers
from apps.UMM.models import CommissionCategory


class CommissionCategorySerializer(serializers.ModelSerializer):
    """提成分类序列化器"""
    
    def validate_commission_ratio(self, value):
        """验证提成比例"""
        # 确保提成比例是有效的小数，并且在0-1之间
        try:
            value = float(value)
            if value < 0 or value > 1:
                raise serializers.ValidationError("提成比例必须在0到1之间(对应0%-100%)")
            return value
        except (ValueError, TypeError):
            raise serializers.ValidationError("提成比例必须是有效的数值")
    
    class Meta:
        model = CommissionCategory
        fields = ['id', 'commission_category', 'commission_ratio']
        read_only_fields = ['id'] 